/*
 * @(#)TestSort.java
 *
 * Summary: Demonstrate how to sort a Collection in Various orders using Sun's Collections.sort.
 *
 * Copyright: (c) 2009 Roedy Green, Canadian Mind Products, http://mindprod.com
 *
 * Licence: This software may be copied and used freely for any purpose but military.
 *          http://mindprod.com/contact/nonmil.html
 *
 * Requires: JDK 1.6+
 *
 * Created with: IntelliJ IDEA IDE.
 *
 * Version History:
 *  1.0 2009-01-01
 */
package com.mindprod.example;

import java.awt.Color;
import java.util.ArrayList;
import java.util.Collections;

import static java.lang.System.out;

/**
 * Demonstrate how to sort a Collection in Various orders using Sun's Collections.sort.
 *
 * @author Roedy Green, Canadian Mind Products
 * @version 1.0 2009-01-01
 * @since 2009-01-01
 * http://mindprod.com/jgloss/sort.html
 */
public class TestSort
    {
    // --------------------------- main() method ---------------------------

    public static void main( String[] args )
        {
        // build ArrayList of Animals
        ArrayList<Animal> animals = new ArrayList<Animal>( 5 );
        animals.add( new Animal( "panther", Color.BLACK, 4 ) );
        animals.add( new Animal( "frog", Color.GREEN, 4 ) );
        animals.add( new Animal( "crab", Color.RED, 10 ) );
        animals.add( new Animal( "elephant", Color.GRAY, 2 ) );
        animals.add( new Animal( "spider", Color.BLACK, 8 ) );

        //out.println( "*** sort in Natural Order (by horns)" );
        Collections.sort( animals );
        for ( Animal animal : animals )
            {
            //out.println( animal.toString() );
            }

        //out.println( "*** sort in Reverse Natural Order (by horns)" );
        Collections.sort( animals, Collections.reverseOrder() );
        for ( Animal animal : animals )
            {
            //out.println( animal.toString() );
            }

        //out.println( "*** sort alphabetically" );
        Collections.sort( animals, new Animal.Alphabetically() );
        for ( Animal animal : animals )
            {
            //out.println( animal.toString() );
            }

        //out.println( "*** sort reverse alphabetically" );
        Collections.sort( animals, Collections.reverseOrder( new Animal.Alphabetically() ) );
        for ( Animal animal : animals )
            {
            //out.println( animal.toString() );
            }

        out.println( "*** sort by color" );
        Collections.sort( animals, new Animal.ByRGB() );
        for ( Animal animal : animals )
            {
            out.println( animal.toString() );
            }
        }
    }

